package com.data.structure.node;

/**
 * @author zflu
 * 循环队列
 * Created by lucky on 2018-12-10.
 */
public class RoundQueue {

    private long[] a;

    private int size;

    private int nItems;
    /**
     * 头
     */
    private int front;
    /**
     * 尾
     */
    private int rear;

    public RoundQueue(int maxSize){
        this.size = maxSize;
        this.a = new long[size];
        this.front = 0;
        this.rear = -1;
        this.nItems = 0;
    }

    public void insert(long v){
        if(isFull()){
            System.out.println("队列已经满了!");
            return;
        }
        //存入尾部
        rear = ++rear % size;
        a[rear] = v;
        nItems++;
    }

    /**
     * 移除头
     * @return
     */
    public long remove(){
        if(isEmpty()){
            System.out.println("队列为空!");
            return 0;
        }
        nItems --;
        front = front % size;
        return a[front++];
    }

    /**
     * 出列
     * @return
     */
    public long peek(){
        if(isEmpty()){
            System.out.println("队列为空!");
            return 0;
        }
        return a[front];
    }

    /**
     * 获取大小
     * @return
     */
    public int size(){
        return nItems;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty(){
        return (nItems == 0);
    }

    /**
     * 判断队列是否满
     * @return
     */
    public boolean isFull(){
        return (size == nItems);
    }

    public void display(){
        if(isEmpty()){
            System.out.println("队列为空!");
        }
        int iteam = front;
        for (int i = 0 ; i < nItems ; i ++){
            System.out.println(a[iteam++ % size]);
        }
    }

    public static void main(String[] args) {
        RoundQueue queue = new RoundQueue(6);
        queue.insert(1);
        queue.insert(2);
        queue.insert(3);
        queue.insert(4);
        queue.insert(5);
        queue.insert(6);
        queue.remove();
        queue.display();
    }
}
